package com.ymatou.validatecode.api.testcase;

import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.validatecode.api.parameter.GetValidateCodeBean;
import com.ymatou.validatecode.api.service.GetValidateCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ValidateCodeService;
import com.ymttest.database.model.YmtValidationCode;
import com.ymttest.database.sqlwapper.YmtValidationCodeWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*获取验证码（字符串）用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetValidateCode{
	private static GetValidateCodeBean getvalidatecodeBean;
	private static GetValidateCodeCall getvalidatecodeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取验证码（字符串）用例");
	}

	@Before
	public void caseUp() {
		getvalidatecodeBean=new GetValidateCodeBean();
		getvalidatecodeCall=new GetValidateCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getvalidatecodeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetValidateCode_001() {
		Logger.start(true,"获取数字和字母混合类型验证码-长度4位");
		try {
			getvalidatecodeBean=new ValidateCodeService().setGetValidateCodeBean(0, 60, 4, TestDataManager.getRandomNum(10), TestDataManager.getRandomIntNum(2));

			getvalidatecodeCall.setData(getvalidatecodeBean);
			getvalidatecodeCall.callService();			
			
			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetValidateCode_002() {
		Logger.start(true,"获取纯数字类型验证码-长度4位");
		try {

			getvalidatecodeBean=new ValidateCodeService().setGetValidateCodeBean(1, 60, 4, TestDataManager.getRandomNum(10), TestDataManager.getRandomIntNum(2));
			
			getvalidatecodeCall.setData(getvalidatecodeBean);
			getvalidatecodeCall.callService();			

			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetValidateCode_003() {
		Logger.start(true,"获取纯字母类型验证码-长度4位");
		try {

			getvalidatecodeBean=new ValidateCodeService().setGetValidateCodeBean(2, 60, 4, TestDataManager.getRandomNum(10), TestDataManager.getRandomIntNum(2));
			
			getvalidatecodeCall.setData(getvalidatecodeBean);
			getvalidatecodeCall.callService();			

			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetValidateCode_004() {
		Logger.start(true,"获取GUID类型验证码-长度4位");
		try {

			getvalidatecodeBean=new ValidateCodeService().setGetValidateCodeBean(3, 60, 4, TestDataManager.getRandomNum(10), TestDataManager.getRandomIntNum(2));
			
			getvalidatecodeCall.setData(getvalidatecodeBean);
			getvalidatecodeCall.callService();			

			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetValidateCode_005() {
		Logger.start(true,"获取数字和字母混合类型验证码-长度64位-32位以上只需要支持validationCodeType1或者2");
		try {
			getvalidatecodeBean=new ValidateCodeService().setGetValidateCodeBean(0, 60, 64, TestDataManager.getRandomNum(10), TestDataManager.getRandomIntNum(2));

			getvalidatecodeCall.setData(getvalidatecodeBean);
			getvalidatecodeCall.callService();			

			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetValidateCode_006() {
		Logger.start(true,"获取纯数字类型验证码-长度64位");
		try {

			getvalidatecodeBean=new ValidateCodeService().setGetValidateCodeBean(1, 60, 64,TestDataManager.getRandomNum(10), TestDataManager.getRandomIntNum(2));
			
			getvalidatecodeCall.setData(getvalidatecodeBean);
			getvalidatecodeCall.callService();			

			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetValidateCode_007() {
		Logger.start(true,"获取纯字母类型验证码-长度64位");
		try {

			getvalidatecodeBean=new ValidateCodeService().setGetValidateCodeBean(2, 60, 64, TestDataManager.getRandomNum(10), TestDataManager.getRandomIntNum(2));
			
			getvalidatecodeCall.setData(getvalidatecodeBean);
			getvalidatecodeCall.callService();			

			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetValidateCode_008() {
		Logger.start(true,"获取GUID类型验证码-长度64位");
		try {

			getvalidatecodeBean=new ValidateCodeService().setGetValidateCodeBean(3, 60, null, TestDataManager.getRandomNum(10), TestDataManager.getRandomIntNum(2));
			
			getvalidatecodeCall.setData(getvalidatecodeBean);
			getvalidatecodeCall.callService();			

			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetValidateCode_009() {
		Logger.start(true,"10分钟内重复获取的时候，直接返回当前数据源中已经存在的验证码");
		try {
			getvalidatecodeBean = new ValidateCodeService().setGetValidateCodeBean();	
			
			String valicateCode1 = new ValidateCodeService().GetValidateCode(getvalidatecodeBean).getValidateCode();

			getvalidatecodeCall.setData(getvalidatecodeBean);
			getvalidatecodeCall.callService();
			
			Logger.verifyEquals(ECode.SUCCESS, getvalidatecodeCall.getCode(), "验证Code");

			String valicateCode2 = getvalidatecodeCall.getValidateCode();
			
			Logger.verifyEquals("200", getvalidatecodeCall.getCode(), "验证返回code");
			Logger.verifyEquals("", getvalidatecodeCall.getMessage(), "验证返回Message");

			Logger.verifyEquals(valicateCode1, valicateCode2, "验证10分钟内重复获取的时候，直接返回当前数据源中已经存在的验证码");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	@Ignore
	public void Tc_GetValidateCode_010() {
		Logger.start(true,"10分钟后重复获取的时候，返回新的验证码");
		try {
			getvalidatecodeBean = new ValidateCodeService().setGetValidateCodeBean();	
			
			String valicateCode1 = new ValidateCodeService().GetValidateCode(getvalidatecodeBean).getValidateCode();

			Thread.sleep(700000);
			getvalidatecodeCall.setData(getvalidatecodeBean);
			getvalidatecodeCall.callService();
			
			Logger.verifyEquals(ECode.SUCCESS, getvalidatecodeCall.getCode(), "验证Code");

			String valicateCode2 = getvalidatecodeCall.getValidateCode();
			
			Logger.verifyEquals("200", getvalidatecodeCall.getCode(), "验证返回code");
			Logger.verifyEquals("", getvalidatecodeCall.getMessage(), "验证返回Message");

			Logger.verifyNotEquals(valicateCode1, valicateCode2, "10分钟后重复获取的时候，返回新的验证码");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetValidateCode_011() {
		Logger.start(true,"过期时间默认10分钟");
		try {

			getvalidatecodeBean=new ValidateCodeService().setGetValidateCodeBean(3, null, 64, "123456", 11);
			
			getvalidatecodeCall.setData(getvalidatecodeBean);
			getvalidatecodeCall.callService();		
			
			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyReturn()
	{
		Logger.verifyEquals("200", getvalidatecodeCall.getCode(), "验证返回code");
		Logger.verifyEquals("", getvalidatecodeCall.getMessage(), "验证返回Message");

		String validateCode = getvalidatecodeCall.getValidateCode();		

		if(getvalidatecodeBean.getLength()!=null&&getvalidatecodeBean.getValidationCodeType()!=0&&getvalidatecodeBean.getValidationCodeType()!=3)
		{
			Logger.verifyEquals(getvalidatecodeBean.getLength(), validateCode.length(), "验证返回验证码长度");
		}
		
		// 验证数据库落地正确
		YmtValidationCode validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(getvalidatecodeBean.getActionUser(), getvalidatecodeBean.getValidateType()).get(0);
		
		Logger.verifyEquals(validateCode, validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
		Logger.verifyEquals(0, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
		Logger.verifyEquals(0, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");
		
		Date createTime = validationCode.getCreatedTime();
		Date expireTime = validationCode.getExpiredTime();
		long m = ((expireTime.getTime()-createTime.getTime())/(1000));
		
		if(getvalidatecodeBean.getExpiredSeconds()!=null)
		{
			Logger.verifyEquals(getvalidatecodeBean.getExpiredSeconds(), (int)m, "验证数据库表Ymt_ValidationCode中字段iValidateResult");
		}
		else {
				Logger.verifyEquals(600, (int)m, "验证数据库表Ymt_ValidationCode中字段ExpiredTime");
		}
		
		Logger.verifyEquals(true,(new Date().getTime()-createTime.getTime())/(1000*60)<1, "验证数据库表Ymt_ValidationCode中字段CreatedTime");

	}
}